﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.SqlClient;
using KinConquer.Sockets;
using KinSocket;

namespace KinConquer
{
    public class Program
    {
        public unsafe static void Main(string[] args)
        {
            Console.Title = "Auth - KinConquer";

            Program.Database = new KinConquer.Database();
            Program.Database.ExecuteNonQuery("TRUNCATE TABLE authenticate");

            AuthSocket auth = new AuthSocket();

            Console.WriteLine("Online");
            while (true)
            {
                Console.ReadLine();
                /*foreach (KinSocket.Connection con in AuthConnections.EnumerableValues)
                {
                    if (con.ReceiveQueue.Count > 0)
                    {
                        Byte[] Data = con.ReceiveQueue.Dequeue();
                        fixed (byte* pData = Data)
                        {
                            Packets.Delegates.PacketHandles[BitConverter.ToUInt16(Data, 2)](pData, con);
                        }
                    }
                }
                Thread.Sleep(10);*/
            }
            Program.Database.Dispose();
        }

        public static Counter AuthConID = new Counter(1, 99999);

        public static Database Database;

        public static DictionaryV2<UInt32, KinSocket.Connection> AuthConnections
            = new DictionaryV2<UInt32, KinSocket.Connection>();

        public unsafe static void OnTimer(Connection con)
        {
            con.DoQueueSend();
            if (con.ReceiveQueue.Count > 0)
            {
                Byte[] Data = con.ReceiveQueue.Dequeue();
                fixed (byte* pData = Data)
                {
                    Packets.Delegates.PacketHandles[BitConverter.ToUInt16(Data, 2)](pData, con);
                }
            }
        }
    }
}
